package com.itheima.service;

import com.itheima.pojo.Account;
import com.itheima.pojo.Arrangement;
import com.itheima.pojo.Movie;
import com.itheima.pojo.ShowMovie;

import java.util.ArrayList;
import java.util.Scanner;

/*
* 账户的业务处理类
* */
public class AccountService {
//  用户对象集合(包括管理人员和普通用户)
    ArrayList<Account> accounts = new ArrayList<>();

    Scanner sc = new Scanner(System.in);
//    后台管理对象
    ManagerService managerService = new ManagerService();
//    客户端对象
    Client client = new Client();
//用于接收后台排片的ShowMovie对象的集合
    ArrayList<ShowMovie> showMovies = new ArrayList<>();

//注册功能
    public void register(){
        String username;
        System.out.println("============注册账户============");
        while (true) {
            System.out.println("请输入用户名:");
            username = sc.next();
            Account account = getAccountByUsername(username);

            if(account == null){
                break;
            }else {
                System.out.println("该用户名已被占用,请重试!");
            }
        }

        //提示并输入密码
        System.out.println("请输入密码:");
        String password = sc.next();
        String role;
        while (true) {
            System.out.println("请选择你的角色:1.用户\t2.管理员");
            String choice = sc.next();

            if("1".equals(choice)){
                role = "USER";
                break;
            }else if("2".equals(choice)){
                System.out.println("请输入管理员密码:");
                String adminPassword = sc.next();
                if("8".equals(adminPassword)){
                    role = "ADMIN";
                    break;
                }else {
                    System.out.println("很抱歉,管理员密码错误,请重试!");
                }
            }else {
                System.out.println("您的选择有误,请重试!");
            }

        }

        Account account = new Account(username,password,role);
        accounts.add(account);
        System.out.println("注册成功!");

    }

    /*
    * 根据指定的用户名查询对象
    * @return没有查到就返回NULL*/
    public Account getAccountByUsername(String username){
        for (int i = 0; i < accounts.size(); i++) {
            Account account = accounts.get(i);
            if(username.equals(account.getUsername())){
                return account;
            }
            
        }
        return null;

    }


    /**
     * 3.账户登录功能
     */
    public void login() {
        System.out.println("==========账户登录==========");
        //3.1 提示并输入用户名和密码
        System.out.println("请输入您的用户名：");
        String username = sc.next();
        System.out.println("请输入您的密码：");
        String password = sc.next();

        //3.2 先校验用户名：调用getAccountByUsername方法查询该用户名对应的Account账户对象
        Account account = getAccountByUsername(username);
        //如果查询到的账户对象为null，用户名错误！如果查询到的账户对象不为null，就要比对密码
        if (account == null || !account.getPassword().equals(password)) {
            //条件成立：用户名或密码错误
            System.out.println("很抱歉，用户名或密码错误，请检查后重试！");
            return;
        }

        //3.3 获取Account对象的角色，根据角色判断是进入客户端还是后台管理端
        String role = account.getRole();
        if ("USER".equals(role)) {
            System.out.println("进入客户端...");

            ArrayList<Arrangement> arrayList = managerService.m.arrange.arrayList;
            ArrayList<Movie> list = managerService.m.list;
            client.start(account,showMovies,arrayList,list);
        } else {
            System.out.println("进入后台管理端...");
            managerService.start(account,accounts,showMovies);

        }
    }

}
